<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\models\AgentConfig;
use app\models\KeyMap;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $agent app\models\Agent
 */

ApiAsset::register($this);
LayerAsset::register($this);

$this->title = '配置代理商';
$this->params['breadcrumbs'][] = '用户管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('', 'post', ['class' => 'form form-inline']);?>
<div class="form-group required">
    <label class="control-label" for="multi_agent">是否可以开下级代理</label>
    <?php echo Html::radioList('multi_agent', AgentConfig::getConfig($agent->id, 'multi_agent'), KeyMap::getValues('yes_no'), ['id' => 'multi_agent']);?>
	<label class="control-label" for="comm_agent">选择代理返佣类型</label>
	<?php echo Html::dropDownList('comm_agent', AgentConfig::getConfig($agent->id, 'comm_agent'), KeyMap::getValues('comm_agent'), ['id' => 'comm_agent', 'class' => 'form-control', 'prompt' => '返佣类型']);?>
    <div class="help-block"></div>
    <button type="button" onclick="saveAgentConfig(<?php echo $agent->id;?>, 'multi_agent', $('[name=multi_agent]:checked').val(), 'comm_agent', $('#comm_agent').val())" class="btn btn-sm btn-success">保存</button>
</div>
<?php echo Html::endForm();?>
<script>
    /**
     * 保存代理商设置
     * @param agentId integer 代理商编号
     * @param k string 键
     * @param v string 值
     */
    function saveAgentConfig(agentId, k, v, kt, vt) {
        $.getJSON('<?php echo Url::to(['/admin/user/save-agent-config']);?>', {agent_id: agentId, k: k, v: v, kt: kt, vt: vt}, function (json) {
            if (callback(json)) {
                layer.msg('数据已保存。');
            }
        });
    }
</script>
